package pers.qianyu.month_202012.date_20201214;

import java.util.*;

/**
 * 646. 最长数对链
 * https://leetcode-cn.com/problems/maximum-length-of-pair-chain/
 *
 * @author mizzle rain
 * @date 2020-12-14 16:41
 */
public class FindLongestChain {
    /**
     * 动态规划
     */
    public int findLongestChain(int[][] pairs) {
        if (pairs == null) {
            return 0;
        }
        int len = pairs.length;
        if (len == 1) {
            return 1;
        }
        Arrays.sort(pairs, (arr1, arr2) -> arr1[0] - arr2[0]);
        // dp[i] 表示以 pairs[i] 结尾的最长数对有 dp[i] 个
        int[] dp = new int[len];
        Arrays.fill(dp, 1);
        for (int i = 1; i < pairs.length; i++) {
            for (int j = 0; j < i; j++) {
                if (pairs[i][0] > pairs[j][1]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }
        int res = 0;
        for (int i : dp) {
            res = Math.max(i, res);
        }
        return res;
    }
}
